requireJS(['constant', 'site', 'handlebars', 'public'], function (Constant, site, Handlebars, Public) {
    var url = {
        queryProductAll: "manager/queryProductAll.do",
        fileUploader: "manager/fileUploader.do",
        importDataProduct: "manager/importDataProduct.do",
        importDataProductData: "manager/importDataProductData.do"
    }

    var pageNum = 1;
    var data;
    var file;
    var fileArr

    function queryProduct() {
        var frontData = {
            pageNum: pageNum
        }
        site.send({
            url: url.queryProductAll,
            data: frontData,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#product_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var list = data.content.list;
                    var html = template(list);
                    $('#product_list').html(html);
                    $('.product_winBox').show();

                    $('.product-page-container').createPager({
                        current: frontData.pageNum,
                        total: data.content.total,
                        pageSize: data.content.pageSize,
                        needInput: false,
                        backFn: function (current) {
                            pageNum = current;
                            queryProduct();
                        }
                    })
                }
            }
        })

    }

    function queryProductSelect(){
        var frontData = {
            pageNum: pageNum
        }
        site.send({
            url: url.queryProductAll,
            data: frontData,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#product_select_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var list = data.content.list;
                    var html = template(list);
                    $("#product_select_list").html(html);
                }
            }
        })
    }

    queryProductSelect()


    $("#templateSelect").on('click', function () {
        queryProduct();
    })

    $('.confirm').on('click', function () {
        var id = $("input[name='product_radio']:checked").val()
        if (!id) {
            Constant.fn.Message('I', '请选择对应的产品模板')
            return;
        }
        window.open(localStorage.getItem('host') + url.importDataProduct + '?product_id=' + id);
        $('.product_winBox').hide();
    })

    //$('.confirm').on('click', function () {
        //$('.product_winBox').hide();
    //})

    function upLoadFile() {
        fileArr = [];
        file = $('<input type="file" name="file" id="files"/>')
        var form = $('<form></form>')
        file.appendTo(form);
        file.click();
        file.change(function () {
            var files = this.files;
            for (var i = 0; i < files.length; i++) {
                fileArr.push(files[i]);
            }
        })
    }

    $("#picker").on('click', function () {
        var id = $("#product_select_list").val()
        if (!id) {
            Constant.fn.Message('I', '请选择产品')
            return;
        }
        upLoadFile();
    })

    $('#upload').on('click', function () {
        var formData = new FormData();
        //console.log(file.files)
        formData.append("file", fileArr[0]);

        site.send({
            url: url.fileUploader,
            data: formData,
            //type: "POST",
            //crossDomain: true,
            /* xhrFields: {
                 withCredentials: true
             },*/
            processData: false,//用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    Constant.fn.Message("T", "上传成功")
                    site.send({
                        url: url.importDataProductData,
                        data: {
                            url: data.msg,
                            product_id: $("#product_select_list").val()
                        },
                        callback:function(data){

                        }
                    })
                }
            },
        })
    })

})